% GAUTIER LE BIHAN - 2020
% Replication files for “Shocks vs Menu Costs: Patterns of Price Rigidity in an Estimated Multi-Sector Menu-Cost Model�? –Review of Economics and Statistics
% File: moments_stat_kurto.m 
% Generates Table 1 in paper

clear all
close all
clear matrix
clc


%%turn	turn_ns	turn_posi	turn_posi_ns	promo	soldes	f_sales	dprix	dprix_p5	dprix_p10	dprix_p25	dprix_p40	dprix_p50	dprix_p60	dprix_p75	dprix_p90	dprix_p95	dprix_std	dprix_ns	dprix_p5_ns	dprix_p10_ns	dprix_p25_ns	dprix_p40_ns	dprix_p50_ns	dprix_p60_ns	dprix_p75_ns	dprix_p90_ns	dprix_p95_ns	dprix_std_ns	dprix_pos	dprix_neg_ns	dprix_pos_ns	indic_small	idposte_p	p_c5	pond_c5

mat_actual_moments=xlsread('..\..\moments\stat_94_2014_kurto.xlsx') ;
names_of_actual_moments={'p_c5','pond_c5','idposte1','m_kur','m_interq','m_q2','s_kur','s_interq','s_q2','fr_p','f_p','f','sfr_p','sf_p','sf','idposte_p','secteur'};

%choose moments to match with simulated moments%
my_list_actual={'secteur';'f';'fr_p';'m_q2';'m_interq';'m_kur';'sf';'sfr_p';'s_q2';'s_interq';'s_kur'};
n_actual_moments=size(my_list_actual,1);
%%Initialize vector of indices
idxa=zeros( size(my_list_actual,1),1) ;

%%Loop over selected moments
for ii=1:size(my_list_actual,1)
my_name=my_list_actual(ii);
%% This picks up the index corresponding to the selected moments (using string comparison function 'strcmp')
idxa(ii) = find(strcmp([names_of_actual_moments],my_name )) ;
end
       actual_moments=mat_actual_moments(:,idxa);
      % actual_moments=actual_moments(actual_moments(:,1)<0.5,:);
weight=mat_actual_moments(:,2)
sum(weight)
% for ii=1:size(my_list_actual,1)
% actual_moments_w(ii) = weightedMedian(actual_moments(:,ii),weight);
% end
 

test=[actual_moments weight];
sampl_sim=test(( test(:,1) ~= 3 & test(:,1) ~= 0),:);
%sampl_sim=test((test(:,6) == 5 ),:);
%test(:,6) ~= 5 &%

actual_moments_k=[sampl_sim];

%save actual_moments_en actual_moments_en

weight=sampl_sim(:,12);
weight_tot=weight;


save actual_moments_k actual_moments_k

actual_moments=actual_moments_k;
moments_mean=(actual_moments(:,2:6)'*weight_tot)./sum(weight_tot)
 std_mean=(actual_moments(:,7:11)'*weight_tot)./sum(weight_tot)
 
 
 moments_mean=(actual_moments(actual_moments(:,1)~=5,2:6)'*weight_tot(actual_moments(:,1)~=5))./sum(weight_tot(actual_moments(:,1)~=5))
 std_mean=(actual_moments(actual_moments(:,1)~=5,7:11)'*weight_tot(actual_moments(:,1)~=5))./sum(weight_tot(actual_moments(:,1)~=5))
 
std_t1=sqrt(var(actual_moments(:,2:6), weight_tot))

moments_mean=(actual_moments((actual_moments(:,1)~=5 & actual_moments(:,1)~=6),2:6)'*weight_tot((actual_moments(:,1)~=5 & actual_moments(:,1)~=6),:)./sum(weight_tot((actual_moments(:,1)~=5 & actual_moments(:,1)~=6),:)))
 std_mean=(actual_moments((actual_moments(:,1)~=5 & actual_moments(:,1)~=6),7:11)'*weight_tot((actual_moments(:,1)~=5 & actual_moments(:,1)~=6),:)./sum(weight_tot((actual_moments(:,1)~=5 & actual_moments(:,1)~=6),:)))

moments_mean=(actual_moments((actual_moments(:,1)==6),2:6)'*weight_tot(actual_moments(:,1)==6,:)./sum(weight_tot(actual_moments(:,1)==6,:)))
 std_mean=(actual_moments((actual_moments(:,1)==6),7:11)'*weight_tot(actual_moments(:,1)==6,:)./sum(weight_tot(actual_moments(:,1)==6,:)))
  
moments_mean=(actual_moments((actual_moments(:,1)==5),2:6)'*weight_tot(actual_moments(:,1)==5,:)./sum(weight_tot(actual_moments(:,1)==5,:)))
 std_mean=(actual_moments((actual_moments(:,1)==5),7:11)'*weight_tot(actual_moments(:,1)==5,:)./sum(weight_tot(actual_moments(:,1)==5,:)))
 
moments_mean=(actual_moments((actual_moments(:,1)~=5 & actual_moments(:,1)~=6 & actual_moments(:,1)~=1),2:6)'*weight_tot((actual_moments(:,1)~=5 & actual_moments(:,1)~=6 & actual_moments(:,1)~=1),:)./sum(weight_tot((actual_moments(:,1)~=5 & actual_moments(:,1)~=6 & actual_moments(:,1)~=1),:)))
 std_mean=(actual_moments((actual_moments(:,1)~=5 & actual_moments(:,1)~=6 & actual_moments(:,1)~=1),7:11)'*weight_tot((actual_moments(:,1)~=5 & actual_moments(:,1)~=6 & actual_moments(:,1)~=1),:)./sum(weight_tot((actual_moments(:,1)~=5 & actual_moments(:,1)~=6 & actual_moments(:,1)~=1),:)))

 
 moments_mean=(actual_moments((actual_moments(:,1)==1),2:6)'*weight_tot(actual_moments(:,1)==1,:)./sum(weight_tot(actual_moments(:,1)==1,:)))
 std_mean=(actual_moments((actual_moments(:,1)==1),7:11)'*weight_tot(actual_moments(:,1)==1,:)./sum(weight_tot(actual_moments(:,1)==1,:)))
 
 
 stat_mean=[moments_mean std_mean]


%% Computing weighted medians
moments=actual_moments(:,2:11);
wmedw=ones(1,size(moments,2)+2);
v=size(moments,1);
weight_tot=weight;
    for i=1:size(moments,2)
        [sortx,order] = sort(moments(:,i));
        sortw = weight_tot(order);

         midpoint = sum(sortw)/2;
         csumw = cumsum(sortw);
         j = find(csumw<=midpoint,1,'last');
            if csumw(j) == midpoint
              wmedw(1,i) = mean(sortx([j j+1]));
            else
                wmedw(1,i) = sortx(j+1);
            end
     wmedw(1,end)= csumw(end);
     wmedw(1,end-1)= v;
    end  
   fprintf('\n Median aggregate:\n');
     stat_median=[wmedw(1:5)' wmedw(6:10)']
 fprintf('|| %5.4f ',stat_median );
  
 
 
 
 
   moments_1=actual_moments((actual_moments(:,1)~=5),2:11);
weight_1=actual_moments((actual_moments(:,1)~=5),12);
wmedw=ones(1,size(moments_1,2)+2);
v=size(moments_1,1);
weight_tot=weight_1;
    for i=1:size(moments_1,2)
        [sortx,order] = sort(moments_1(:,i));
        sortw = weight_tot(order);

         midpoint = sum(sortw)/2;
         csumw = cumsum(sortw);
         j = find(csumw<=midpoint,1,'last');
            if csumw(j) == midpoint
              wmedw(1,i) = mean(sortx([j j+1]));
            else
                wmedw(1,i) = sortx(j+1);
            end
     wmedw(1,end)= csumw(end);
     wmedw(1,end-1)= v;
    end  
   fprintf('\n Median Sector1:\n');
     stat_median=[wmedw(1:5)' wmedw(6:10)'];
 fprintf('|| %5.4f ',stat_median );




moments_1=actual_moments((actual_moments(:,1)==1),2:11);
weight_1=actual_moments((actual_moments(:,1)==1),12);
wmedw=ones(1,size(moments_1,2)+2);
v=size(moments_1,1);
weight_tot=weight_1;
    for i=1:size(moments_1,2)
        [sortx,order] = sort(moments_1(:,i));
        sortw = weight_tot(order);

         midpoint = sum(sortw)/2;
         csumw = cumsum(sortw);
         j = find(csumw<=midpoint,1,'last');
            if csumw(j) == midpoint
              wmedw(1,i) = mean(sortx([j j+1]));
            else
                wmedw(1,i) = sortx(j+1);
            end
     wmedw(1,end)= csumw(end);
     wmedw(1,end-1)= v;
    end  
   fprintf('\n Median Sector1:\n');
     stat_median=[wmedw(1:5)' wmedw(6:10)'];
 fprintf('|| %5.4f ',stat_median );
    
moments_2=actual_moments((actual_moments(:,1)==2),2:11);
weight_2=actual_moments((actual_moments(:,1)==2),12);

wmedw=ones(1,size(moments_2,2)+2);
v=size(moments_2,1);
weight_tot=weight_2;
    for i=1:size(moments_2,2)
        [sortx,order] = sort(moments_2(:,i));
        sortw = weight_tot(order);

         midpoint = sum(sortw)/2;
         csumw = cumsum(sortw);
         j = find(csumw<=midpoint,1,'last');
            if csumw(j) == midpoint
              wmedw(1,i) = mean(sortx([j j+1]));
            else
                wmedw(1,i) = sortx(j+1);
            end
     wmedw(1,end)= csumw(end);
     wmedw(1,end-1)= v;
    end  
   fprintf('\n Median Sector2:\n');
   stat_median=[wmedw(1:5)' wmedw(6:10)'];
 fprintf('|| %5.4f ',stat_median );
    
  
moments_2=actual_moments((actual_moments(:,1)==4),2:11);
weight_2=actual_moments((actual_moments(:,1)==4),12);

wmedw=ones(1,size(moments_2,2)+2);
v=size(moments_2,1);
weight_tot=weight_2;
    for i=1:size(moments_2,2)
        [sortx,order] = sort(moments_2(:,i));
        sortw = weight_tot(order);

         midpoint = sum(sortw)/2;
         csumw = cumsum(sortw);
         j = find(csumw<=midpoint,1,'last');
            if csumw(j) == midpoint
              wmedw(1,i) = mean(sortx([j j+1]));
            else
                wmedw(1,i) = sortx(j+1);
            end
     wmedw(1,end)= csumw(end);
     wmedw(1,end-1)= v;
    end  
   fprintf('\n Median Sector4:\n');
   
   stat_median=[wmedw(1:5)' wmedw(6:10)'];
 fprintf('|| %5.4f ',stat_median );  
     

moments_2=actual_moments((actual_moments(:,1)==5),2:11);
weight_2=actual_moments((actual_moments(:,1)==5),12);

wmedw=ones(1,size(moments_2,2)+2);
v=size(moments_2,1);
weight_tot=weight_2;
    for i=1:size(moments_2,2)
        [sortx,order] = sort(moments_2(:,i));
        sortw = weight_tot(order);

         midpoint = sum(sortw)/2;
         csumw = cumsum(sortw);
         j = find(csumw<=midpoint,1,'last');
            if csumw(j) == midpoint
              wmedw(1,i) = mean(sortx([j j+1]));
            else
                wmedw(1,i) = sortx(j+1);
            end
     wmedw(1,end)= csumw(end);
     wmedw(1,end-1)= v;
    end  
   fprintf('\n Median Sector 5:\n');
   stat_median=[wmedw(1:5)' wmedw(6:10)'];
 fprintf('|| %5.4f ',stat_median );
     
     
     
moments_2=actual_moments((actual_moments(:,1)==6),2:11);
weight_2=actual_moments((actual_moments(:,1)==6),12);

wmedw=ones(1,size(moments_2,2)+2);
v=size(moments_2,1);
weight_tot=weight_2;
    for i=1:size(moments_2,2)
        [sortx,order] = sort(moments_2(:,i));
        sortw = weight_tot(order);

         midpoint = sum(sortw)/2;
         csumw = cumsum(sortw);
         j = find(csumw<=midpoint,1,'last');
            if csumw(j) == midpoint
              wmedw(1,i) = mean(sortx([j j+1]));
            else
                wmedw(1,i) = sortx(j+1);
            end
     wmedw(1,end)= csumw(end);
     wmedw(1,end-1)= v;
    end  
   fprintf('\n Median Sector6:\n');
     
   stat_median=[wmedw(1:5)' wmedw(6:10)'];
 fprintf('|| %5.4f ',stat_median );
     
     

     